package net.yeah.mickey_zhouyou.module.booklist.ui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

import net.yeah.mickey_zhouyou.module.booklist.biz.CategoryManager;
import net.yeah.mickey_zhouyou.module.booklist.biz.impl.CategoryManagerImpl;
import net.yeah.mickey_zhouyou.module.booklist.util.PopupMenuFactory;

public class CategoryPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6442381130440887832L;

	private CategoryPane() {
		super.setLayout(new BorderLayout());

		final JTree tree = categoryManageBiz.loadTree();
		super.add(new JScrollPane(tree), BorderLayout.CENTER);

		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					TreePath path = tree.getPathForLocation(e.getX(), e.getY());
					if (path == null) {
						PopupMenuFactory.getCategoryTreeBlankMenu(tree).show(
								tree, e.getX(), e.getY());
					} else {
						tree.setSelectionPath(path);
						PopupMenuFactory.getCategoryTreeNodeMenu(tree).show(
								tree, e.getX(), e.getY());
					}
				}
			}
		});
	}

	private static final CategoryPane pane = new CategoryPane();

	public static CategoryPane getPane() {
		return pane;
	}

	private CategoryManager categoryManageBiz = CategoryManagerImpl
			.getInstance();

}
